<?php
	namespace Qwadmin\Controller;
	use Think\Controller;
	class HeadersallController extends ComController{
		public $hdb;
		public $ldb;
		public $vdb;
		public $idb;
		public function __construct(){
			parent::__construct();

			$this->hdb=M('Headers_all');
			$this->ldb=M('Lines_all');
			$this->vdb=M('Vendors');
			$this->idb=M('Item_no');
		}
		public function itemno($code){
			$where['item_no']=$code;
			$info=$this->idb->where($where)->select();
			return $info[0];
		}
		public function vendors($code){
			$where['vendor_code']=$code;
			$info=$this->vdb->where($where)->select();
			return $info[0];
		}
		public function addhead(){
			$post['vendor_code']=I('post.vendor_code');
			if($post['vendor_code']){
				$nd=I('post.need_date');
				$post['need_date']=strtotime($nd);
				$post['note']=I('post.note');
				$post['created_by']=$this->uid();
				$post['creation_date']=time();
				$post['po_num']=$this->date();
				$post['status']="INPROCESS";
				$info=$this->hdb->add($post);
				$linefor=I('post.stockid');
				if($info){
					$allamount=0;
					$code=$post['po_num'];
					foreach ($linefor as $key => $value) {
						if($value){
							$add['po_num']=$code;
							$add['stockid']=$value;
							$add['quantity']=$_POST['quantity'][$key];
							$add['line']=$_POST['number'][$key];
							$add['uom']=$_POST['uom'][$key];
							$add['price']=$_POST['price'][$key];
							$add['note']=$_POST['notee'][$key];
							$add['amount']=(int)$add['price']*(int)$add['quantity'];
							$allamount=$allamount+$add['amount'];
							$add['created_by']=$this->uid();
							$add['creation_date']=time();
							$add['need_date']=$post['need_date'];
							$add['status']="INPROCESS";
							$this->ldb->add($add);
						}
					}
					$whe['po_num']=$code;
					$this->hdb->where($whe)->setField("amount",$allamount);

				}
			}else{
				$this->display();
			}
		}
		public function editline(){//修改数量
			$num=(int)I('get.num');
			$where['line']=I('get.line');
			$where['po_num']=I('get.po_num');
			$ba=$this->idb->where($where)->setField('quantity',$num);
			if($ba){
				$re['status']=true;
				$re['con']=$num;
			}else{
				$re['status']=false;
			}
			$this->ajaxReturn($re);
		}
		public function addline(){
			$add['po_num']=I('post.po_num');
			$add['quantity']=I('post.quantity');//数目
			$add['line']=$this->pocount($add['po_num']);
			$hinfo=$this->hinfo($add['po_num']);
			$add['need_date']=$hinfo['need_date'];
			$add['created_by']=$this->uid();
			$add['creation_date']=time();
			$add['stockid']=I('post.stockid');//料号
			$add['uom']=I('post.uom');//单位
			$add['price']=I('post.price');//价格
			$add['note']=I('post.notee');
			$add['status']="INPROCESS";
			$re['status']=$this->idb->add($add);
			if($re['status']){
				$re['con']=$add;
			}
			$this->ajaxReturn($add);
		}
		protected function hinfo($ponum){
			$where['po_num']=$ponum;
			$this->hdb->where($where)->select();
		}
		protected function pocount($ponum){
			$where['po_num']=$ponum;
			$back=$this->ldb->where($where)->count();
			return $back[0];
		}
		public function delline(){
			$where['po_num']=I('get.po_num');
			$where['line']=I('get.line');
			$ba=$this->ldb->where($where)->setField('status','cancel');
			if($ba){
				$re['status']=true;
			}else{
				$re['status']=false;
			}
			$this->ajaxReturn($re);
		}
		public function change(){
			$id=I('get.id');
			$post['vendor_code']=I('post.vendor_code');
			if($post['vendor_code']){
				$nd=I('post.need_date');
				$post['need_date']=strtotime($nd);
				$post['note']=I('post.note');
				$post['last_update_by']=$this->uid();
				$post['last_updated_date']=time();
				$post['po_num']=$id;
				$post['status']="INPROCESS";
				$whereh['po_num']=$post['po_num'];
				$info=$this->hdb->where($whereh)->save($post);
				//var_dump($info);
				$linefor=I('post.stockid');
				if($info){
					$code=$post['po_num'];
					$li=0;
					$allamount=0;
					foreach ($linefor as $key => $value) {
						if($value){
							$add['po_num']=$code;
							$add['stockid']=$value;
							$add['quantity']=$_POST['quantity'][$key];
							$add['line']=$_POST['number'][$key];
							$add['uom']=$_POST['uom'][$key];
							$add['price']=$_POST['price'][$key];
							$add['note']=$_POST['notee'][$key];
							$add['created_by']=$this->uid();
							$add['creation_date']=time();
							$add['need_date']=$post['need_date'];
							$add['amount']=(int)$add['price']*(int)$add['quantity'];
							$add['status']="INPROCESS";
							$li=$li+1;
							$allamount=$allamount+$add['amount'];
							$where['line']=$add['line'];
							$where['po_num']=$code;
							$ju=$this->ldb->where($where)->select();
							if($ju){
								$this->ldb->where($where)->save($add);
							}else{
								$this->ldb->add($add);
							}
							$whe['po_num']=$id;
							$this->hdb->where($whe)->setField("amount",$allamount);
						}
					}
				}
			}else{
				$where['po_num']=I('get.id');
				$info=$this->hdb->where($where)->select();
				$foi=$this->ldb->where($where)->select();
				$foi=$this->jugg($foi);
				$count=$this->ldb->where($where)->count();
				$count++;
				$this->assign('count',$count);
				$this->assign('head',$info[0]);
				$this->assign('main',$foi);
				$this->display();
			}
		}
		protected function jugg($info){
			foreach ($info as $key => $value) {
				if($value['status']!='cancel'){
					$ba[]=$value;
				}
			}
			return $ba;
		}	
		public function uid(){
			$uid=$_SESSION['uid'];
			$uid=(int)$uid;
			$info=M('Member')->find($uid);
			return $info['user'];
		}
		public function date(){
			$today=strtotime(date('Y-m-d'));
			//echo $today;
			$where['creation_date']=array('gt',$today);
			$ba=$this->hdb->where($where)->count();
			$no=$ba+1;
			return  "PO".date('Ymd').$no;

		}






	}